/**
 * 抽奖小程序
 * author: chuchur / chuchur@qq.com
 */
 ; (function (global) {
    var utils = {
        hasClass: function (elements, cName) {
            return !elements ? false : !!elements.className.match(new RegExp("(\\s|^)" + cName + "(\\s|$)"));
        },
        addClass: function (elements, cName) {
            if (!utils.hasClass(elements, cName)) {
                elements && (elements.className += " " + cName)
            };
        },
        removeClass: function (elements, cName) {
            if (utils.hasClass(elements, cName)) {
                elements && (elements.className = elements.className.replace(new RegExp("(\\s|^)" + cName + "(\\s|$)"), ""))
            }
        }
    };
    var luckDraw = function (option) {
        option = option || {}
        if (!option.id) {
            return new Error('请指定轮子的ID')
        }
        if (!option.data || !option.data.length) {
            return new Error('请设置奖品')
        }
        var options = {
            data: [], //奖品的数据
            index: -1, //当前转动到哪个位置，起点位置
            count: 0,//总共有多少个位置
            timer: 0, //setTimeout的ID，用clearTimeout清除
            speed: 300,//初始转动速度
            times: 0,//转动次数
            cycle: 50,//转动基本次数：即至少需要转动多少次再进入抽奖环节
            prize: -1,//中奖位置
            subCls: '', //子集的className带点
            isBengin: false, //开始抽奖的时候就不能循环的抽奖了
            callBack: null,//轮子转完了得弹个什么框的
            autoLuck: false // 默认是否点中间的start 开始抽奖
        }
        option.data.sort(function (a, b) { return a.index - b.index })
        var _this = this;
        options = Object.assign(options, option)
        _this.prize = options.prize || -1

        /**
         * 初始化数据
         */
        var __dom = document.getElementById(option.id)
        var __inner = document.createElement('ul')
        __inner.className = 'luck-draw'

        var star_btn = document.createElement('li')
        star_btn.className = 'luck-draw-item luck-draw-start'
        star_btn.addEventListener('click', function () { if (option.autoLuck) _this.start() })
        let prizes = [];
        [0, 1, 2, 7, 3, 6, 5, 4].forEach(function (i) {
            var item = options.data[i]
            var luck = document.createElement('li')
            luck.className = 'luck-draw-item'
            luck.innerHTML = '<div class="luck-draw-item-icon"><img src="' + item.icon + '" alt=""></div><span class="luck-draw-item-name">' + item.title + '</span></div>'
            prizes.push(luck)
        })
        prizes.splice(4, 0, star_btn)
        prizes.forEach(function (c) {
            __inner.appendChild(c)
        })
        __dom.appendChild(__inner)
        /**
         * 滚动轮子
         */
        var prizeIndexs = { 2: 4, 5: 7, 8: 6, 7: 5, 6: 2, 3: -1 }
        var roll = function () {
            var index = options.index
            utils.removeClass(__inner.childNodes[index], 'luck-draw-item-active')

            index = prizeIndexs[index] || index
            index += 1;

            utils.addClass(__inner.childNodes[index], 'luck-draw-item-active')
            options.index = index;
        }

        /**
        * 开始抽奖
        */
        var winIndex = {
            0: 1, 1: 2, 2: 3, 5: 4, 8: 5, 7: 6, 6: 7, 3: 8
        }
        this.start = function () {
            if (!option.isBengin) {
                this._start()
            }
        }
        this._start = function () {
            clearTimeout(options.timer);
            options.isBengin = true;
            var times = options.times

            times += 1;
            roll(); //转动过程调用的是luckDraw的roll方法，这里是第一次调用初始化
            console.log(options.index, _this.prize, times, options.cycle)
            if (times > options.cycle + 10 && _this.prize == winIndex[options.index]) {
                clearTimeout(options.timer);
                // options.index = -1;
                times = 0;
                options.isBengin = false;
                options.callBack && options.callBack()
            } else {
                if (times < options.cycle) {
                    options.speed -= 10;
                } else if (times == options.cycle) {
                    // var index = Math.random() * (options.count) | 0; //中奖物品通过一个随机数生成
                    // _this.prize = 5;
                } else {
                    if (times > options.cycle + 10 && ((_this.prize == 0 && options.index == 7) || _this.prize == options.index + 1)) {
                        options.speed += 110;
                    } else {
                        options.speed += 20;
                    }
                }
                if (options.speed < 40) {
                    options.speed = 40;
                }
                options.timer = setTimeout(_this._start, options.speed) //循环调用
            }
            options.prize = _this.prize
            options.times = times
        }
    }
    if (typeof module !== 'undefined' && typeof exports === 'object') {
        module.exports = luckDraw
    } else if (typeof define === 'function' && (define.cmd || define.amd)) {
        define(luckDraw)
    } else {
        global.luckDraw = luckDraw;
    }
})(typeof window !== 'undefined' ? window : global);